package todlu.RssReader;

import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.text.Html;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

final class SumaryItemHolder {
	public ImageView markIcon;
	public ImageView pic;
	public ImageView framePic;
	public TextView title;
	public TextView updateTime;
	public TextView description;
	public FrameLayout imageLayout;
}

public class SumaryAdapter extends BaseAdapter {
	private List<HashMap<String, Object>> list;
	private Context context;
	private boolean bRead = false;
	private boolean hasImage = false;
	private SumaryItemHolder holder;
	private LayoutInflater inflater;
	
	public SumaryAdapter(Context context, List<HashMap<String, Object>> list,
			boolean bRead, boolean hasImage) {
		this.context = context;
		this.list = list;
		this.bRead = bRead;
		this.hasImage = hasImage;
		inflater = LayoutInflater.from(context);
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		holder = null;
		//String tmp = list.get(position).get("AddItem").toString();
		
		if (convertView == null) {
			holder = new SumaryItemHolder();

			convertView = inflater.inflate(R.layout.channel_list_item, null);

			holder.markIcon = (ImageView) convertView
					.findViewById(R.id.markicon);
			holder.title = (TextView) convertView
					.findViewById(R.id.sumary_title);
			holder.updateTime = (TextView) convertView
					.findViewById(R.id.summary_date);
			holder.description = (TextView) convertView
					.findViewById(R.id.summary_desc);
			holder.imageLayout = (FrameLayout) convertView
					.findViewById(R.id.summary_picture);
			holder.pic = (ImageView) convertView.findViewById(R.id.summary_pic);
			holder.framePic = (ImageView) convertView
					.findViewById(R.id.summary_picFrame);

			convertView.setTag(holder);

			if (!bRead) {
				holder.markIcon.setImageResource(R.drawable.unread_icon);
			} else {
				holder.markIcon.setImageResource(R.drawable.read_icon);
			}

			holder.title.setText((String) list.get(position).get("Title"));
			holder.updateTime.setText((String) list.get(position).get("Date"));
			Spanned tmp = Html.fromHtml((String) list.get(position).get("Description"));
			
			String tmpText = tmp.toString().replaceAll("￼", "");
			
			tmpText = tmpText.replaceAll("\n", "");
			holder.description.setText(tmpText.toString());

			// TODO set image here

				holder.imageLayout.setVisibility(View.GONE);
		} 
		else 
		{
			holder = (SumaryItemHolder) convertView.getTag();

			if (!bRead) {
				holder.markIcon.setImageResource(R.drawable.unread_icon);
			} else {
				holder.markIcon.setImageResource(R.drawable.read_icon);
			}
			
			holder.title.setText((String) list.get(position).get("Title"));
			holder.updateTime.setText((String) list.get(position).get("Date"));
			Spanned tmp = Html.fromHtml((String) list.get(position).get("Description"));
			String tmpText = tmp.toString().replaceAll("￼", "");
			tmpText = tmpText.replaceAll("\n", "");
			holder.description.setText(tmpText.toString());
			
			//TODO set image here
			holder.imageLayout.setVisibility(View.GONE);
		}

		return convertView;
	}

}
